/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package simplemetrosystem.view.canvas.MouseHandlers;

import java.awt.Cursor;
import java.awt.event.MouseEvent;
import simplemetrosystem.control.EditCommandManager;
import simplemetrosystem.control.ElementManager;
import simplemetrosystem.model.Element.MetroStation;
import simplemetrosystem.view.MetroFrame;
import simplemetrosystem.view.canvas.CanvasModel;
import simplemetrosystem.view.canvas.MetroCanvas;

/**
 *
 * @author Eric Qian
 */
public class NewEdgeHandler extends BasicMouseHandler {

    public NewEdgeHandler(MetroFrame frame) {
        super(frame);
        this.frame = frame;
        pressed = false;
        x1 = 0;
        y1 = 0;
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        super.mouseClicked(e);
        int x = MouseTrans.X(e.getX());
        int y = MouseTrans.Y(e.getY());
        CanvasModel model = CanvasModel.getInstance();
        MetroCanvas canvas = frame.getMetroTabbedPane().getAllLineCanvas();

        if (model.isStation(x, y)) {
            if (pressed) {
                pressed = false;
                MetroStation stA = (MetroStation) model.getCanvasElement(
                        MouseTrans.X(x1), MouseTrans.Y(y1));
                MetroStation stB = (MetroStation) model.getCanvasElement(x, y);

                if (stA != stB) {
                    EditCommandManager.getInstance().addEdge(
                            stA.getName(), stB.getName(),
                            ElementManager.getInstance().getLineAt(0).getName());
                }

                canvas.setNewEdge(false);
                canvas.setNewEdgeLine(0, 0, 0, 0);
            } else {
                pressed = true;
                x1 = e.getX();
                y1 = e.getY();
                canvas.setNewEdge(true);
            }
        } else {
            pressed = false;
            canvas.setNewEdge(false);
            canvas.setNewEdgeLine(0, 0, 0, 0);

        }
    }

    @Override
    public void mouseMoved(MouseEvent e) {
        super.mouseMoved(e);
        if (pressed) {
            MetroCanvas canvas = frame.getMetroTabbedPane().getAllLineCanvas();
            canvas.setNewEdgeLine(x1, y1, e.getX(), e.getY());
        }
    }

    @Override
    public void mousePressed(MouseEvent e) {
        super.mousePressed(e);
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        super.mouseReleased(e);
    }

    @Override
    public void mouseDragged(MouseEvent e) {
        super.mouseDragged(e);
    }

    @Override
    public void mouseEntered(MouseEvent e) {
        super.mouseEntered(e);
        frame.setCursor(crossCursor);
        if (pressed) {
            frame.getMetroTabbedPane().getAllLineCanvas().setNewEdge(true);
        }
    }

    @Override
    public void mouseExited(MouseEvent e) {
        super.mouseExited(e);
        frame.setCursor(defaultCursor);
        frame.getMetroTabbedPane().getAllLineCanvas().setNewEdge(false);
    }
    private MetroFrame frame;
    private boolean pressed;
    private int x1;
    private int y1;
    private static Cursor crossCursor = new Cursor(Cursor.CROSSHAIR_CURSOR);
}
